// pages/worker/worker.js

const config = require("../../utils/config.js")
const api = require('../../utils/api.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    scanResult: "",
    hideScan: false,
    hideLock: true,

    lastId: '',
    problems: '---',
    time_left: '---',
    unlockRequest: '申请开锁',
    unlockDisable: false,
    tmpStyle: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onScan: function () {
    var that = this;
    var scanResult;
    wx.scanCode({
      success: (res) => {
        scanResult = res.result

        if (scanResult.length != 12) {
          wx.showToast({
            title: '请扫描设备上的二维码',
            icon: 'none',
            duration: 1500
          })
          return
        }

        wx.showToast({
          title: '扫描成功',
          icon: 'success',
          mask: true,
          duration: 500,
          success: function () {
            that.setData({
              scanResult: scanResult,
              hideScan: true,
              hideLock: false
            })
          }
        })
      },
      fail: (res) => {
        wx.showToast({
          title: '扫描失败,请重新扫描',
          icon: 'none',
          duration: 1000
        })
      },
      complete: (res) => {}
    })
  },


  onSendUnlockRequest: function () {
    var that = this
    var info = {}
    wx.showModal({
      title: '提示',
      content: '确定发送开锁申请吗?',
      success(res) {
        if (res.confirm) {
          // send request of unlock
          info.device_name = that.data.scanResult;
          info.phone = wx.getStorageSync('phone')
          api.get('request_unlock', info).then(res2 => {
            console.log("request_unlock: ", res2)
            that.data.lastId = res2.lastId
          })

          wx.showToast({
            title: '已发送',
            mask: true,
            duration: 1500,
          })

          wx.showLoading({
            title: '正在处理,请稍候...',
          })

          var inter = setInterval(function () {
            api.get('getProblems', {
              'worker_phone': wx.getStorageSync('phone'),
              'device_name': that.data.scanResult,
              'lastId': that.data.lastId
            }).then(res => {
              var res2 = res
              console.log("getProblems: res: ", res)
              that.setData({
                problems: res.problems,
                time_left: res.timeLeft
              })

              // there are problems
              if (res.problems === 'notyet') {

              } else if (res.problems === "ok") {
                clearInterval(inter)
                wx.hideLoading()
                wx.showToast({
                  title: '开锁申请已通过',
                  mask: true,
                  duration: 3000
                })
              }

              // there are problems
              else {
                clearInterval(inter)
                console.log("probs: ", res.problems)
                wx.hideLoading()
                var strData = info.device_name + ';' + info.phone + ";" + res.problems
                wx.navigateTo({
                  url: '../worker_problems/worker_problems?data=' + strData,
                })
              }
            })
          }, 3000)

          that.setData({
            unlockRequest: "申请开锁",
            hideScan: false,
            hideLock: true,
            unlockDisable: false,
            // tmpStyle: "color:black"
            tmpStyle: ""
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  }


})